package com.zjs.realmController;

import com.zjs.mapper.SysAuthFunctionMapper;
import com.zjs.mapper.SysAuthRoleMapper;
import com.zjs.mapper.SysUserMapper;
import com.zjs.pojo.*;
import com.zjs.service.BcSysUserServiceI;
import com.zjs.service.SysAuthFunctionI;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @类名： ZjsRealm
 * @说明： TODO
 * @作者：SS 苏帅出品
 * @时间：2019/12/13 17:39
 * @版本：1.0
 */
@Component
public class ZjsRealm extends AuthorizingRealm {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    BcSysUserServiceI bcSysUserServiceI;

    @Autowired
    SysAuthRoleMapper sysAuthRoleMapper;

    @Autowired
    SysAuthFunctionI sysAuthFunctionI;

    @Autowired
    SysAuthFunctionMapper sysAuthFunctionMapper;


    @Override
    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        SysUser primaryPrincipal = (SysUser) principalCollection.getPrimaryPrincipal();

        //得到用户角色 通过userid
        String[] roleId = bcSysUserServiceI.findRoleId(primaryPrincipal.getId());

        //添加角色
        List<String> roles=new ArrayList<>();

        for (String role:roleId){

            SysAuthRole sysAuthRole = sysAuthRoleMapper.selectByPrimaryKey(role);

            if (null!=sysAuthRole){

                roles.add(sysAuthRole.getCode());

            }

        }
        //添加权限

        String[] funIdByRoleIs = sysAuthFunctionI.findFunIdByRoleIs(roleId);

        List<String> permissions=new ArrayList<>();

        for (String funId:funIdByRoleIs){

            SysAuthFunction sysAuthFunction = sysAuthFunctionMapper.selectByPrimaryKey(funId);

            if (null!=sysAuthFunction){

                permissions.add(sysAuthFunction.getCode());

            }
        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        //添加该角色
        simpleAuthorizationInfo.addRoles(roles);

        //添加该用户的权限
        simpleAuthorizationInfo.addStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }

    @Override
    //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //得到在subject中传的UsernamePassword对象
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;

        //得到用户名和密码
        String username = token.getUsername();

        //将得到的用户名带入数据库查询 查看是否有具体的数据
        SysUserExample example=new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);

        //得到的是一个list集合只取第一个
        List<SysUser> sysUsers = sysUserMapper.selectByExample(example);

        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo();
        //判断 list集合是否有对象
        if (sysUsers.size()>0 && sysUsers!=null){
             //得到 第一个SysUser对象
            SysUser sysUser = sysUsers.get(0);
            //创建simpleAuthenticationInfo对象  每个参数的参数值 sysUser表示登录成功后使用 getPrincipal()得到的值
           info=new SimpleAuthenticationInfo(sysUser,sysUser.getPassword(),username);

        }

        return info;
    }
}
